﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DK.RingLinkedList;

public class JosephRing
{
    int start;
    int size;
    int skip;
    RingSingleLinkedList<int> linkList;

    public int Start { get => start; private set => start = value; }
    public int Size { get => size; private set => size = value; }
    public int Skip { get => skip; private set => skip = value; }
    public RingSingleLinkedList<int> LinkList { get => linkList; private set => linkList = value; }

    public JosephRing(int start, int size, int skip)
    {
        this.Start = start;
        this.Size = size;
        this.Skip = skip;
        this.linkList = new RingSingleLinkedList<int>();
        for (int i = 0; i < Size; i++)
        {
            linkList.Add(i+1);
        }
    }

    public int[] PopOrder() 
    {
        int[] result = new int[Size];
        var first = linkList.FristNode;

        for (int i = 0; i < Start; i++)
        {
            first = first?.Next;
        }

        var rindex = 0;
        do
        {
            for (int i = 0; i < Skip - 1; i++)
            {
                first = first?.Next;
            }
            result[rindex] = linkList.Delete(first.Index).Data ;
            first = first?.Next;
            rindex++;
        } while (rindex < Size);

        return result;
    }
}

